package com.shire.shire_interface.service.timeline;

import com.shire.shire_interface.dao.timeline.WxTimelineTaskDao;
import com.shire.shire_interface.model.command.WxCommand;
import com.shire.shire_interface.model.timeline.WxTimelineTask;
import com.shire.shire_interface.model.timeline.WxTimelineTaskExample;
import com.shire.shire_interface.model.user.WxAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WxTimelineTaskService {
    @Autowired
    private WxTimelineTaskDao timelineTaskDao;

    /***
     * 取待发送的朋友圈任务
     * @param account
     * @return
     */
    public List<WxTimelineTask> getWxTimelineTaskToSend(WxAccount account){
        WxTimelineTaskExample taskExample = new WxTimelineTaskExample();
        taskExample.createCriteria().andWxaccountEqualTo(account.getWxid()).andStatusEqualTo(0).andCmdidIsNull();
        return timelineTaskDao.selectByExample(taskExample);
    }

    public List<WxTimelineTask> getWxTimelineTask(WxTimelineTaskExample example){
        return timelineTaskDao.selectByExample(example);
    }

    public int inserTimelineTask(WxTimelineTask timelineTask){
        return timelineTaskDao.insert(timelineTask);
    }

    public WxTimelineTask getTimelineById(Integer id){
        return timelineTaskDao.selectByPrimaryKey(id);
    }

    public int updateWxTimelineTask(WxTimelineTask task){
        return timelineTaskDao.updateByPrimaryKeySelective(task);
    }
    public int updateTimelineTaskByCommand(WxCommand command){
        WxTimelineTaskExample taskExample = new WxTimelineTaskExample();
        taskExample.createCriteria().andCmdidEqualTo(command.getId().intValue());

        List<WxTimelineTask> taskList = timelineTaskDao.selectByExample(taskExample);
        if (taskList == null || taskList.size() <1){
            return -1;
        }
        WxTimelineTask task = taskList.get(0);
        task.setCmdstatus(command.getRststatus());
        task.setUpdatetime(new Date());

        return timelineTaskDao.updateByPrimaryKeySelective(task);
    }
}
